/*
 * @(#) CodeListTag .java  1.0  2004-4-12
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */
package com.cnc.tag;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import com.cnc.ssm.ISsmService;

/**
 * User: hongdj
 * Date: 2008-7-2
 */
public class CodeInfoListTag extends LtcmTag {


    protected boolean disabled = false;
    private boolean condition = false;
    private String value = "";
	private String codeType="";

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public boolean isCondition() {
        return condition;
    }

    public void setCondition(boolean condition) {
        this.condition = condition;
    }

    public boolean getDisabled() {
        return (this.disabled);
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
    public String getCodeType() {
        return codeType;
    }

    public void setCodeType(String codeType) {
        this.codeType = codeType;
    }


    public int doEndTag() throws JspException {

        StringBuffer results = new StringBuffer();

        ISsmService ssmService = (ISsmService) this.getBean(com.cnc.bas.util.Constants.BEAN_NAME_SSM);
        String codeType = this.codeType;
        List codeInfo = ssmService.listCodeInfo(codeType);
        Iterator iter = codeInfo.iterator();
        if (condition) {
            results.append("<option value=\"");
            results.append("\"");
            if (disabled) {
                results.append(" disabled=\"disabled\"");
            }
            results.append(">");

            results.append("</option>");
        }
        while (iter.hasNext()) {
            HashMap codeTypeMap = (HashMap) iter.next();
            results.append("<option value=\"");
            String codeValue = String.valueOf(codeTypeMap.get("CODE"));
            String codeName = String.valueOf(codeTypeMap.get("NAME"));
            results.append(codeValue.trim());
            results.append("\" ");
            if (disabled) {
                results.append(" disabled=\"disabled\"");
            }

            SelectTag selecttag = this.selectTag();
            if (selecttag != null) {
                if (selecttag.isMatched(codeValue.trim())) {
                    results.append(" selected=\"selected\"");
                }
            }

            results.append(">");
            results.append(codeName.trim());
            results.append("</option>");

        }
        ResponseUtils.write(pageContext, results.toString());
        return (EVAL_PAGE);
    }

    private SelectTag selectTag() throws JspException {
        SelectTag selectTag =
                (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);

        if (selectTag == null) {
            JspException e =
                    new JspException("error : no select element");

            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        return selectTag;
    }


}
